// OpentTxl-C Version 11 safe C dynamic arrays
// Copyright 2023, James R. Cordy and others

#include <stdlib.h>

// Safe dynamic array allocation

// Usage:  array(type, name);
#define array(type, name)   type *name

// Usage:  arrayalloc (upper, type, name);
#define arrayalloc(size, type, name) \
    { \
        name = (type *) (malloc((size) * sizeof (type))); \
        if (name == (type *) 0) { \
            fprintf (stderr, "TXL ERROR : (Fatal) Available heap space too small for TXL heap at this size\n"); \
            exit (1); \
        } \
        /* _alloc_allocs[_alloc_nextalloc++] = (void *) (name); */ \
    }

// Safe non-scalar assignment
#define arrayassign(dest,src)   memcpy((void *)&(dest), (void *)&(src), (sizeof(dest)))
#define structassign(dest,src)  memcpy((void *)&(dest), (void *)&(src), (sizeof(dest)))
